package com.xmtlxx.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.enums.UserType;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.system.domain.bo.SysWorklogBo;
import com.xmtlxx.system.domain.vo.SysWorklogVo;
import com.xmtlxx.system.service.ISysWorklogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 流程日志Controller
 *
 * @author xmtlxx
 * @date 2022-04-24
 */
@Validated
@Api(value = "流程日志控制器", tags = {"流程日志管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/sysWorklog")
public class SysWorklogController extends BaseController {

    private final ISysWorklogService iSysWorklogService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<SysWorklogVo> initBean(){
        SysWorklogVo vo = new SysWorklogVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询流程日志列表
     */
    @ApiOperation("查询流程日志列表")
    @SaCheckPermission("datas:query:worklog")
    @GetMapping("/list")
    public TableDataInfo<SysWorklogVo> list(@Validated(QueryGroup.class) SysWorklogBo bo, PageQuery pageQuery) {
//        if(LoginHelper.getUserType().equals(UserType.SYS_USER)){
//            bo.setUserId(LoginHelper.getUserId());
//            bo.setDeptId(LoginHelper.getDeptId());
//        }else
            if(LoginHelper.getUserType().equals(UserType.PATROL)){
            bo.setUserId(LoginHelper.getUserId());
        }
        return iSysWorklogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出流程日志列表
     */
    @ApiOperation("导出流程日志列表")
    @SaCheckPermission("system:sysWorklog:export")
    @Log(title = "流程日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated SysWorklogBo bo, HttpServletResponse response) {
        List<SysWorklogVo> list = iSysWorklogService.queryList(bo);
        ExcelUtil.exportExcel(list, "流程日志", SysWorklogVo.class, response);
    }

    /**
     * 获取流程日志详细信息
     */
    @ApiOperation("获取流程日志详细信息")
    @SaCheckPermission("datas:query:worklog")
    @GetMapping("/{logid}")
    public R<SysWorklogVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("logid") String logid) {
        return R.ok(iSysWorklogService.queryById(logid));
    }

    /**
     * 新增流程日志
     */
    @ApiOperation("新增流程日志")
    @SaCheckPermission("system:sysWorklog:add")
    @Log(title = "流程日志", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysWorklogVo vo) {
        SysWorklogBo bo = new SysWorklogBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysWorklogService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改流程日志
     */
    @ApiOperation("修改流程日志")
    @SaCheckPermission("system:sysWorklog:edit")
    @Log(title = "流程日志", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysWorklogVo vo) {
        SysWorklogBo bo = new SysWorklogBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysWorklogService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除流程日志
     */
    @ApiOperation("删除流程日志")
    @SaCheckPermission("system:sysWorklog:remove")
    @Log(title = "流程日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{logids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] logids) {
        return toAjax(iSysWorklogService.deleteWithValidByIds(Arrays.asList(logids), true) ? 1 : 0);
    }
}
